package com.want.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.extern.slf4j.Slf4j;

/**
 * @author WangZhiJian
 * @since 2021/11/28 0028
 */
@Slf4j
public class MyBeanUtil {

    public static <T> T  copyProperties(Object source,Class<T> clazz){
        try {
            T t = clazz.newInstance();
            BeanUtil.copyProperties(source,t);
            return t;
        } catch (Exception e) {
            log.error("使用com.want.util.MyBeaUtil.copyProperties出现异常",e);
            throw new RuntimeException(e);
        }
    }

    public static final CopyOptions copyProperties;
    static {
        copyProperties = new CopyOptions();
        copyProperties.setIgnoreNullValue(true);
    }

    public static <T> T  copyPropertiesIngoreNull(Class<T> clazz,Object... source){
        try {
            T t = clazz.newInstance();
            for (Object o : source){
                BeanUtil.copyProperties(o,t,copyProperties);
            }
            return t;
        } catch (Exception e) {
            log.error("使用com.want.util.MyBeaUtil.copyProperties出现异常",e);
            throw new RuntimeException(e);
        }
    }
}
